import requests,time,hashlib,unittest
class demo(unittest.TestCase):
    def setUp(self):
        self.Time = str(int(time.time()))
        self.Md5 = hashlib.md5()
        self.api_key='&Guest-Bugmaster'
        self.M = self.Time+self.api_key
        self.Md5.update(self.M.encode("utf-8"))
        self.sign = self.Md5.hexdigest()
        #print(self.sign)
    def test_01(self):#通过
        con = requests.post(url="http://127.0.0.1:8000/api/sec_add_event/",
                            data={"eid":160,"name":"标1".encode(),"limit":200,
                                  "address":"成都孵化园".encode(),"start_time":"2012-1-1 18:00:00",
                                  "time":self.Time,
                                  "sign":self.sign
                                  }
                            )
        r = con.text
        #print(r,1)
        self.assertEqual(r,'{"status": 200, "message": "add event success"}',msg="不通过")
        print("用例通过！")
    def test_02(self):#10021
        con = requests.post(url="http://127.0.0.1:8000/api/sec_add_event/",
                            data={"eid":"","name":"标题6".encode(),"limit":200,
                                  "address":"成都孵化园".encode(),"start_time":"2012-1-1 18:00:00",
                                  "time":self.Time,
                                  "sign":self.sign
                                  }
                            )
        r = con.text
        #print(r,1)
        self.assertEqual(r,'{"status": 10021, "message": "parameter error"}',msg="不通过")
        print("用例通过！")
    def test_03(self):#1023
            con = requests.post(url="http://127.0.0.1:8000/api/sec_add_event/",
                                data={"eid":106,"name":"标题6".encode(),"limit":200,
                                      "address":"成都孵化园".encode(),"start_time":"2012-1-1 18:00:00",
                                      "time":self.Time,
                                      "sign":self.sign
                                      }
                                )
            r = con.text
            #print(r,1)
            self.assertEqual(r,'{"status": 10023, "message": "event name already exists"}',msg="不通过")
            print("用例通过！")
    def test_04(self):#10012
        con = requests.post(url="http://127.0.0.1:8000/api/sec_add_event/",
                            data={"eid":108,"name":"标题7".encode(),"limit":200,
                                  "address":"成都孵化园".encode(),"start_time":"2012-1-1 18:00:00",
                                  "time":self.Time,
                                  "sign":""
                                  }
                            )
        r = con.text
        #print(r,1)
        self.assertEqual(r,'{"status": 10012, "message": "user sign null"}',msg="不通过")
        print("用例通过！")
    def test_05(self):#10024
        con = requests.post(url="http://127.0.0.1:8000/api/sec_add_event/",
                            data={"eid":109,"name":"标题8".encode(),"limit":200,
                                  "address":"成都孵化园".encode(),"start_time":"2012-1-118:00:00",
                                  "time":self.Time,
                                  "sign":self.sign
                                  }
                            )
        r = con.text
        #print(r,1)
        self.assertEqual(r,'{"status": 10024, "message": "start_time format error. It must be in YYYY-MM-DD HH:MM:SS format."}',msg="不通过")
        print("用例通过！")
    def test_06(self):#10014
        con = requests.post(url="http://127.0.0.1:8000/api/sec_add_event/",
                            data={"eid":109,"name":"标题8".encode(),"limit":200,
                                  "address":"成都孵化园".encode(),"start_time":"2012-1-118:00:00",
                                  "time":self.Time,
                                  "sign":"sas"
                                  }
                            )
        r = con.text
        #print(r,1)
        self.assertEqual(r,'{"status": 10014, "message": "user sign error"}',msg="不通过")
        print("用例通过！")
    def test_07(self):#10011
        con = requests.get(url="http://127.0.0.1:8000/api/sec_add_event/",
                            data={"eid":105,"name":"标题6".encode(),"limit":200,
                                  "address":"成都孵化园".encode(),"start_time":"2012-1-1 18:00:00",
                                  "time":self.Time,
                                  "sign":self.sign
                                  }
                            )
        r = con.text
        #print(r,1)
        self.assertEqual(r,'{"status": 10011, "message": "request error"}',msg="不通过")
        print("用例通过！")
    def test_08(self):#10013
        con = requests.post(url="http://127.0.0.1:8000/api/sec_add_event/",
                            data={"eid":110,"name":"标题10".encode(),"limit":200,
                                  "address":"成都孵化园".encode(),"start_time":"2012-1-1 18:00:00",
                                  "time":29,
                                  "sign":self.sign
                                  }
                            )
        r = con.text
        #print(r,1)
        self.assertEqual(r,'{"status": 10013, "message": "user sign timeout"}',msg="不通过")
        print("用例通过！")
    def test_09(self):#10022
        con = requests.post(url="http://127.0.0.1:8000/api/sec_add_event/",
                            data={"eid":101,"name":"标题99".encode(),"limit":200,
                                  "address":"成都孵化园".encode(),"start_time":"2012-1-1 18:00:00",
                                  "time":self.Time,
                                  "sign":self.sign
                                  }
                            )
        r = con.text
        #print(r,1)
        self.assertEqual(r,'{"status": 10022, "message": "event id already exists"}',msg="不通过")
        print("用例通过！")

if __name__ == '__main__':
    unittest.main()